<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class About_us extends MY_Controller {
	function __construct() {
		parent::__construct();
		
		$this->load->model('type_model', '', TRUE);
		$this->load->model('con_common_model', '', TRUE);
		$this->load->model('con_photo_model', '', TRUE);
		$this->load->model('con_team_model', '', TRUE);
		$this->load->model('position_model', '', TRUE);
		$this->load->model('region_model', '', TRUE);
	}
	
	public function index() {
		$data = $this->common();
		$data['menu'] = $this->_get_menu_tabs();
		$data['parent_type'] = $this->parent_type;
		$this->load->view('frontend/layout', $data);
	}
	
	public function common() {
		if ($this->input->is_ajax_request()) {
			$type = $this->input->post("type");
			$request = $this->input->post("request");
			
			eval("\$data = \$this->_" . $type . "(\$type, \$request);");
			
			$data['request'] = $request;
			$this->load->view('frontend/' . $this->parent_type . '/' . $type, $data);
		} else {
			$first_child_menu_tab = $this->type_model->_get_first_child_menu_tab_by_parent_variable($this->parent_type);
			
			$type = $first_child_menu_tab['variable'];
			$request = 'main_page';
			
			eval("\$data = \$this->_" . $type . "(\$type, \$request);");
			
			$data['type'] = $type;
			$data['request'] = $request;
			return $data;
		}
	}
	
	public function _company($type, $request) {
		$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
		$data['content'] = $this->con_common_model->search_by_condition($condition, TRUE);
		
		//$condition['con_id'] = $data['content']['id'];
		
		$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
		
		return $data;
	}
	
	public function _design_team($type, $request) {
		if ($request == 'main_page') {
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			
			
			$data['members'] = $this->con_team_model->search_by_condition($condition);
			
			foreach ($data['members'] as $k => $v) {
				$condition['con_id'] = $v['id'];
				$data['members'][$k]['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
			}
		} elseif ($request == 'sub_page') {
			$id = $this->input->post("id");
			$data['user'] = $this->con_team_model->_get_by_id($id);
			
			$condition['con_id'] = $id;
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$data['pic'] = $this->con_photo_model->search_by_condition($condition, TRUE);
			
			$data['position'] = $this->position_model->_get_by_id($data['user']['position']);
			$data['region'] = $this->region_model->_get_full_region_by_id($data['user']['region']);
			
			
			$condition['type'] = $this->type_model->_get_by('variable', 'history', 'id', TRUE);
			$data['history'] = $this->con_common_model->search_by_condition($condition);
			
			$condition['type'] = $this->type_model->_get_by('variable', 'design', 'id', TRUE);
			$data['design'] = $this->con_common_model->search_by_condition($condition);
		}
		return $data;
	}
	
	public function _events($type, $request) {
		if ($request == 'main_page') {
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$condition['sort_year'] = 'DESC';
			$temp_data = $this->con_common_model->search_by_condition($condition);
			
			foreach ($temp_data as $v) {
				$data['events'][$v['year']][] = $v;
			}
			
		} elseif ($request == 'sub_page') {
			$year = $this->input->post("year");
			$data['year'] = $year;
			
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$condition['year'] = $year;
			$condition['offset'] = 0;
			$condition['per_page'] = 9;
			$data['content'] = $this->con_common_model->search_by_condition($condition);
			/*echo "<pre>";
			print_r($data);*/
			
			
			
			$condition['per_page'] = 6;
			$data['gallery'] = $this->con_photo_model->search_by_condition($condition);
			
			$all_year = $this->con_common_model->_get_all_year_by_type($condition['type']);
			$data['nearest'] = $this->common->_get_array_nearest($all_year, $year);
		}
		return $data;
	}
	
	public function _honor($type, $request) {
		if ($request == 'main_page') {
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$condition['sort_year'] = 'DESC';
			$data['content'] = $this->con_common_model->search_by_condition($condition);
			
			$condition['offset'] = 0;
			$condition['per_page'] = 6;
			$data['gallery'] = $this->con_photo_model->search_by_condition($condition);
			
		}
		return $data;
	}
	
	public function _manager_community($type, $request) {
		$condition['is_manager'] = TRUE;
		$manager = $this->con_team_model->search_by_condition($condition, TRUE);
		unset($condition['is_manager']);
		
		$sub_page_type = $this->input->post("sub_page_type");
		$data['sub_page_type'] = $sub_page_type;
		
		if ($request == 'main_page') {
			$condition['is_manager'] = TRUE;
			$data['manager'] = $this->con_team_model->search_by_condition($condition, TRUE);
			
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$data['portrait'] = $this->con_photo_model->search_by_condition($condition, TRUE);
		} elseif ($request == 'sub_page') {
			$condition['is_manager'] = TRUE;
			$data['manager'] = $this->con_team_model->search_by_condition($condition, TRUE);
			
			$condition['type'] = $this->type_model->_get_by('variable', $type, 'id', TRUE);
			$data['portrait'] = $this->con_photo_model->search_by_condition($condition, TRUE);
			
			$condition['pid'] = $this->type_model->_get_by('variable', 'manager_community', 'id', TRUE);
			
			$data['child'] = $this->type_model->search_by_condition($condition);
			$data['child'][] = $this->type_model->_get_by('variable', 'design', '*', TRUE);
			unset($condition['sort_weight']);
			
			foreach ($data['child'] as $k => $v) {
				$condition['type'] = $v['id'];
				$data['child'][$k]['contentInfo'] = $this->con_common_model->search_by_condition($condition);
			}
			//echo "<pre>";
			//print_r($data['child']);
		} elseif ($request == 'flip_sub_page') {			
			$condition['con_id'] = $manager['id'];
			$condition['type'] = $this->type_model->_get_by('variable', $sub_page_type, 'id', TRUE);
			$condition['offset'] = 0;
			
			if ($sub_page_type == 'design') {
				$condition['per_page'] = 14;
			} else {
				$condition['per_page'] = 3;
			}
			
			$data['content'] = $this->con_common_model->search_by_condition($condition);
			
			foreach ($data['content'] as $k => $v) {
				$condition['con_id'] = $v['id'];
				$data['content'][$k]['pic'] = $this->con_photo_model->search_by_condition($condition);
			}
			/*echo "<pre>";
			print_r($data);*/
		} elseif ($request == 'flip_show_detail') {
			$id = $this->input->post("id");
			$data['content'] = $this->con_common_model->_get_by_id($id);
		}
		return $data;
	}
}